<?php

namespace app\service;

/**
 * 发送邮件 短信的中间转换
 */
class SendCodeBridgeService
{


    public static function getLanguage($param)
    {
        $lang = 'en-US';
        $isoCode = isset($param['isoCode']) ? $param['isoCode'] : '';
        if (in_array($isoCode, ['CN', 'MO', 'TW', 'HK'])) {
            $lang = 'zh-CN';
        }
        return $lang;
    }

    public static function getProgrammeByEmail($param)
    {
        $source = $param['vendor'] . "_" . $param['packageName'] . "_" . $param['language'] . "_email_verify_code";
        return self::makeProgrammeFormat($source);
    }

    public static function getChangeAccountProgramme($param)
    {
        $source = $param['vendor'] .  "_" . $param['language'] . "_change_account";
        return self::makeProgrammeFormat($source);
    }

    /**
     * 组合通讯服务用的验证码短信发送内部标识。SeeEasy：包名_短信语言_短信区域_verify_code,其他：厂商_短信语言_短信区域_verify_code
     * @createTime 2022/12/15 10:15
     * @createUser songweihan
     * @updateDesc 修改信息
     * @updateTime 2022/12/15 10:15
     * @updateUser songweihan
     * @param string $location 短信区域 中国内陆-CN 国际/港澳台-US
     * @param string $language 语言类型 只支持简体中文和英语
     * @param string $vendor 厂商ID，默认ENZ
     * @param string $package 包名
     * @return string 内部使用标识
     */
    public static function getProgrammeByPhone($param)
    {
        $language = strstr($param['language'], "zh") ? "zh-CN" : "en-US";
        $tmpStr = "_" . $language . "_CN_verify_code";
        $tmpArr = ["com.antsvision.seeeasy", "com.antsvision.seeeasyf", "com.aismartplatform.apptoolv3"];
        $vendor = $param['vendor'];
        $res = $vendor . $tmpStr;
        if ($vendor === "ENZ" && in_array($param['packageName'], $tmpArr)) {
            $res = $param['packageName'] . $tmpStr;
        }
        return self::makeProgrammeFormat($res);
    }

    public static function makeProgrammeFormat($programme)
    {
        preg_match_all("/[a-zA-Z0-9_]/u", $programme, $tmpArr);
        $res = strtolower(join("", $tmpArr[0]));
        return $res;
    }
}
